package com.nba.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.nba.bean.Hotspot;
import com.nba.bean.inherit.HotInherit;
import com.nba.dao.HotDao;
import com.nba.util.DBUtil;

public class HotDaoImpl implements HotDao {
	
	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	
	@Override
	public List<Hotspot> selectHotAll() {
		String sql = "SELECT * FROM hotspot";
		return jt.query(sql, new BeanPropertyRowMapper<Hotspot>(Hotspot.class));
	}

	@Override
	public Hotspot selectHotById(int hotspotId) {
		String sql = "SELECT * FROM hotspot WHERE hotsopt_id = ?";
		List<Hotspot> list = jt.query(sql, new BeanPropertyRowMapper<Hotspot>(Hotspot.class),hotspotId);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public int insertHot(Hotspot hotspot) {
		String sql = "INSERT INTO hotspot (player_id,hotspot_one,hotspot_two,hotspot_three,hotspot_four,hotspot_five,hotspot_six,hotspot_seven,hotspot_eight,hotspot_nine,hotspot_ten,hotspot_eleven,hotspot_twelve,hotspot_thirteen,hotspot_fourteen,season_id,hotspot_date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		return jt.update(sql, hotspot.getPlayerId(),hotspot.getHotspotOne(),hotspot.getHotspotTwo(),hotspot.getHotspotThree(),hotspot.getHotspotFour(),hotspot.getHotspotFive(),hotspot.getHotspotSix(),hotspot.getHotspotSeven(),hotspot.getHotspotEight(),hotspot.getHotspotNine(),hotspot.getHotspotTen(),hotspot.getHotspotEleven(),hotspot.getHotspotTwelve(),hotspot.getHotspotThirteen(),hotspot.getHotspotFourteen(),hotspot.getSeasonId(),hotspot.getHotspotDate());
	}

	@Override
	public int deleteHot(int hotspotId) {
		String sql = "DELETE FROM hotspot WHERE hotspot_id = ?";
		return jt.update(sql, hotspotId);
	}

	@Override
	public int updateHot(Hotspot hotspot) {
		String sql = "UPDATE hotspot SET player_id = ?,hotspot_one = ?,hotspot_two = ?,hotspot_three = ?,hotspot_four= ?,hotspot_five= ?,hotspot_six = ?,hotspot_seven = ?,hotspot_eight = ?,hotspot_nine = ?,hotspot_ten = ?,hotspot_eleven = ?,hotspot_twelve = ?,hotspot_thirteen = ?,hotspot_fourteen = ?,season_id = ?,hotspot_date = ? WHERE hotspot_id = ?"; 
		return jt.update(sql,  hotspot.getPlayerId(),hotspot.getHotspotOne(),hotspot.getHotspotTwo(),hotspot.getHotspotThree(),hotspot.getHotspotFour(),hotspot.getHotspotFive(),hotspot.getHotspotSix(),hotspot.getHotspotSeven(),hotspot.getHotspotEight(),hotspot.getHotspotNine(),hotspot.getHotspotTen(),hotspot.getHotspotEleven(),hotspot.getHotspotTwelve(),hotspot.getHotspotThirteen(),hotspot.getHotspotFourteen(),hotspot.getSeasonId(),hotspot.getHotspotDate(),hotspot.getHotspotId());
	}

	@Override
	public List<HotInherit> selectHotAll(String keyword, int pageNum, int pageSize) {
		String sql = "SELECT * FROM hps WHERE player_name LIKE ? OR season_name LIKE ? LIMIT ? , ?";
		List<HotInherit> list = jt.query(sql, new BeanPropertyRowMapper<HotInherit>(HotInherit.class),"%"+keyword+"%","%"+keyword+"%",(pageNum-1)*pageSize,pageSize);
		return list;
	}

	@Override
	public int selectHotTotal(String keyword) {
		String sql = "SELECT count(1) FROM hps WHERE player_name LIKE ? OR season_name LIKE ?";
		return jt.queryForObject(sql, Integer.class,"%"+keyword+"%","%"+keyword+"%");
	}

}
